// +----------------------------------------------------------------------
// | Manong.Cloud [ 领酷码农云 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.manong.cloud All rights reserved.
// +----------------------------------------------------------------------
// | Author: 稻草人 <qinuoyun@qq.com>
// +----------------------------------------------------------------------
// | Member: 围棋 飘逸者 Loumt Silence24
// +----------------------------------------------------------------------

class ConfigController extends BasicController {

    async actionGetInit(){
        let init = await this.modules('Area.Init').first();
        if(!init){
            throw new Error("未初始化");
        }
        return init;
    }

    async actionInit(){
        let init = await this.modules('Area.Init').first();
        if(init){
            throw new Error("已初始化");
        }

        let {code} = $_POST

        let area = await this.modules('Area').where({code}).first();
        if(!area){
            throw new Error("地区查询失败");
        }

        return await this.modules('Area.Init').validateCreate({code, level: area.level});
    }

    async actionAddress() {
        return originUrl;
    }

    async actionQrcodeSave() {
        let content = {
            building_qrcode_time: $_POST['building_qrcode_time'] || 0,
            company_qrcode_time: $_POST['company_qrcode_time'] || 0,
            user_qrcode_time: $_POST['user_qrcode_time'] || 0
        }
        return await this.saveSetting('qrcode_setting', content);
    }

    async actionQrcodeView() {
        return this.getSetting('qrcode_setting');
    }

    async actionFlowPersonSave() {
        let content = {
            type: $_POST['type'] || 'street'
        }
        return await this.saveSetting('flow_person_setting', content);
    }

    async actionFlowPersonView() {
        return this.getSetting('flow_person_setting');
    }


    async actionArea() {
        let { province, area, city } = $_POST
        if (!province && !area && !city) {
            return await this.modules('Area').where({ level: 1 }).fields(['code', 'name']).select();
        }

        if (province && !city && !area) {
            return await this.modules('Area').where({ level: 2, province }).fields(['code', 'name']).select();
        }

        if (province && city && !area) {
            return await this.modules('Area').where({ level: 3, province, city }).fields(['code', 'name']).select();
        }

        if (province && city && area) {
            return await this.modules('Area').where({ level: 4, province, city, area }).fields(['code', 'name']).select();
        }
        throw new Error("参数缺失");
    }


}

export default ConfigController;